package co.fitstart.entity.order;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.DynamicUpdate;
import com.demo2do.core.format.DecimalFormat;
import com.demo2do.core.utils.DateFormatUtils;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "moyu_orders")
@DynamicUpdate
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class MoyuOrder {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 订单号 */
    private String code;
    
    /** 快递单号 */
    private String wuliuCode;
    
    private int amount;
    
    private String type;
    
    private String name;
    
    private String mobile;
    
    private String pcd;
    
    private String address;
    
    /** 单价 */
    @DecimalFormat
    private int singlePrice;
    
    /** 订单金额 金额*100 */
    @DecimalFormat
    private int price;
    
    /** 实付金额 */
    private int paidPrice;
    
    /** 创建时间/下单时间 */
    private Date createTime;
    
    /** 支付时间 */
    private Date payTime;
    
    /** 支付流水号 */
    private String payCode;
    
    /** 备注 */
    private String remarks;
    
    /** 订单状态 */
    @Enumerated(EnumType.ORDINAL)
    private MoyuOrderStatus orderStatus;
    
    private String openid;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    public MoyuOrder() {
        // TODO Auto-generated constructor stub
    }
    
    public MoyuOrder(int amount, String type, 
            String name, String mobile, 
            String pcd, String address, 
            int singlePrice,
            String openid) {
        this.code = this.gnerateCode();
        this.amount = amount;
        this.type = type;
        this.name = name;
        this.mobile = mobile;
        this.pcd = pcd;
        this.address = address;
        this.singlePrice = singlePrice;
        this.price = new BigDecimal(amount).multiply(new BigDecimal(singlePrice)).intValue();
        this.paidPrice = 0;
        this.createTime = new Date();
        this.orderStatus = MoyuOrderStatus.CREATED;
        this.openid = openid;
        this.disabled = false;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String gnerateCode() {
        
        // 2 digits random number between 0 (inclusive) ~ 100 (exclusive)
        String randomNumber = String.format("%02d", Double.valueOf(Math.floor(Math.random() * 100)).intValue());
        
        // Generate code with 20 digits total
        return new StringBuilder().append("RMJMY")
                                  .append(randomNumber)
                                  .append(DateFormatUtils.format(new Date(), "yyMMddHHmmssSSS"))
                                  .toString();
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public int getPayingPrice() {
        return this.price - this.paidPrice;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }
    
    public String getWuliuCode() {
        return wuliuCode;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the pcd
     */
    public String getPcd() {
        return pcd;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @return the singlePrice
     */
    public int getSinglePrice() {
        return singlePrice;
    }

    /**
     * @return the price
     */
    public int getPrice() {
        return price;
    }

    /**
     * @return the paidPrice
     */
    public int getPaidPrice() {
        return paidPrice;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the payTime
     */
    public Date getPayTime() {
        return payTime;
    }

    /**
     * @return the payCode
     */
    public String getPayCode() {
        return payCode;
    }

    /**
     * @return the remarks
     */
    public String getRemarks() {
        return remarks;
    }

    /**
     * @return the orderStatus
     */
    public MoyuOrderStatus getOrderStatus() {
        return orderStatus;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }
    
    public String getOpenid() {
        return openid;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }
    
    public void setWuliuCode(String wuliuCode) {
        this.wuliuCode = wuliuCode;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param pcd the pcd to set
     */
    public void setPcd(String pcd) {
        this.pcd = pcd;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @param singlePrice the singlePrice to set
     */
    public void setSinglePrice(int singlePrice) {
        this.singlePrice = singlePrice;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(int price) {
        this.price = price;
    }

    /**
     * @param paidPrice the paidPrice to set
     */
    public void setPaidPrice(int paidPrice) {
        this.paidPrice = paidPrice;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param payTime the payTime to set
     */
    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    /**
     * @param payCode the payCode to set
     */
    public void setPayCode(String payCode) {
        this.payCode = payCode;
    }

    /**
     * @param remarks the remarks to set
     */
    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    /**
     * @param orderStatus the orderStatus to set
     */
    public void setOrderStatus(MoyuOrderStatus orderStatus) {
        this.orderStatus = orderStatus;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
    public void setOpenid(String openid) {
        this.openid = openid;
    }
}
